package com.tools.commons.response;

import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * @author wangminggen
 */
@Getter
public enum ResponseCode implements Assert {
    /*
       成功，操作成功返回200 和 success
     */
    SUCCESS(HttpStatus.OK, "成功"),
    SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "服务端错误"),
    BAD_REQUEST(HttpStatus.BAD_REQUEST, "{0}"),
    METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "不支持当前请求方式"),
    UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "未登陆、或者登陆失效"),
    DEV_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "{0}"),
    ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "token过期"),
    REFRESH_TOKEN_EXPIRED(HttpStatus.FORBIDDEN, "登陆状态失效"),
    ;

    private final int code;
    private final String i18nKey;
    private final String defaultMsgTemplate;

    ResponseCode(HttpStatus httpStatus, String defaultMsgTemplate) {
        this(httpStatus, null, defaultMsgTemplate);
    }

    ResponseCode(int code, String defaultMsgTemplate) {
        this(code, null, defaultMsgTemplate);
    }

    /**
     * 做国际化使用此构造函数
     *
     * @param code               接口返回值
     * @param i18nKey            国际化时读取 i18nKey 的 value
     * @param defaultMsgTemplate 消息i18nKey为null 时，以此消息为模板
     */
    ResponseCode(int code, String i18nKey, String defaultMsgTemplate) {
        this.code = code;
        this.i18nKey = i18nKey;
        this.defaultMsgTemplate = defaultMsgTemplate;
    }

    ResponseCode(HttpStatus httpStatus, String i18nKey, String defaultMsgTemplate) {
        this(httpStatus.value(), i18nKey, defaultMsgTemplate);
    }

    private static final Logger log = LoggerFactory.getLogger(ResponseCode.class);

    @Override
    public Logger getLog() {
        return log;
    }

    @Override
    public ResponseCode getResponseCode() {
        return this;
    }
}
